<template>
  <div class="">
    <Box>
      <div slot="box">
        <a-row :gutter="16">
          <a-col class="gutter-row" :span="6">
            <div style="width: 100%">
              <a-select v-model="data.type" placeholder="流程类型" style="width: 100%" @change="handleChange1">
                <a-select-option value="">
                  全部
                </a-select-option>
                <a-select-option value="1">
                  案事件任务
                </a-select-option>
                <a-select-option value="2">
                  物资调用
                </a-select-option>
                <a-select-option value="3">
                  启动预案
                </a-select-option>
              </a-select>
            </div>
          </a-col>
          <a-col class="gutter-row" :span="6">
            <div style="width: 100%">
              <a-select v-model="data.processStatus" placeholder="当前审批状态" style="width: 100%" @change="handleChange2">
                <a-select-option value="">
                  全部
                </a-select-option>
                <a-select-option value="2">
                  完成
                </a-select-option>
                <a-select-option value="1">
                  审批中
                </a-select-option>
                <a-select-option value="3">
                  已驳回
                </a-select-option>
                <a-select-option value="4">
                  已撤销
                </a-select-option>
              </a-select>
            </div>
          </a-col>
          <a-col class="gutter-row" :span="6">
            <a-button type="primary" @click="query">查询</a-button>
          </a-col>
        </a-row>
      </div>
    </Box>
    <Box>
      <div slot="box">
        <CustomTable
          ref="table"
          :showAlert="false"
          :columns="columns"
          :dataSource="table"
          :showSelection="false"
          :showPagination="true"
          :customRow="clickCustomRow"
          rowKey="id"
          :loading="loading"
          @change="onTableChange"
        >

          <template slot="processStatus" slot-scope="text, record">
            <a-badge v-if="text == '2'" status="processing" text="完成" />
            <a-badge color="green" v-if="text == '1'" status="processing" text="审批中" />
            <a-badge color="magenta" v-if="text == '3'" status="processing" text="驳回" />
            <a-badge color="#C6CED3" v-if="text == '4'" status="processing" text="已撤销" />
          </template>
          <template slot="operate" slot-scope="text, record">
            <a style="margin: 0 5px" href="javascript:;" @click="listLoook(record)">查看</a>
            <a style="margin: 0 5px" v-if="record.processStatus == '1'" href="javascript:;" @click="chexiao(record)">撤销</a>
            <a style="margin: 0 5px;color:#eb2f96 " v-if="record.processStatus == '3'" href="javascript:;" @click="chongxin(record)">重新发起</a>
          </template>
        </CustomTable>
      </div>
    </Box>
    <a-modal v-model="visible2" title="物资调用" @ok="handleOk2" width="1000px" centered>
      <!--        <Input ref="input"></Input>-->
      <transfer ref="transfer" :id="id" :storageLocation="storageLocation" :interface="interface" @objData="objData" :informationInterface="informationInterface"></transfer>
    </a-modal>
  </div>
</template>

<script>
import CustomTable from '@/components/zxd/Table/CustomTable'
import Box from '@views/xlgc/Box'
import { getDictItemsFromCache } from '@api/api'
import { getAllList, getMineList, putCancel, putChongfa } from '@api/xlgc/staging'
import transfer from '../transfer'
import {reissueDetails} from "@api/xlgc/Res";
export default {
  name: 'List',
  components: { CustomTable, Box,transfer },
  data() {
    return {
      id:undefined,
      storageLocation:undefined,
      visible2:false,
      value: '',
      dateFormat: 'YYYY-MM-DD',
      data: {
        type: undefined,
        processStatus: undefined
      },
      columns: [
        {
          title: '序号',
          dataIndex: 'order',
          width: 60,
          align: 'center',
          customRender: (text, record, index) =>
            `${(this.pageFilter.pageNo - 1) * this.pageFilter.pageSize + (index + 1)}` // 序号分页后连续
        },
        {
          title: '流程名称',
          dataIndex: 'title',
          scopedSlots: { customRender: 'title' }
        },
        {
          title: '流程类型',
          dataIndex: 'processType',
          scopedSlots: { customRender: 'processType' }
        },
        {
          title: '创建时间',
          dataIndex: 'applyTime',
          scopedSlots: { customRender: 'applyTime' }
          // customRender: (text,record) => {
          //   //字典值翻译通用方法
          //   return this.filterMultiDictText('cases_level', text)
          // }
        },
        {
          title: '当前状态',
          dataIndex: 'processStatus',
          scopedSlots: { customRender: 'processStatus' }
        },
        {
          title: '流程当前节点',
          dataIndex: 'processCurrentNode',
          scopedSlots: { customRender: 'processCurrentNode' }
        },
        {
          title: '操作',
          width: 200,
          align: 'center',
          dataIndex: 'operate',
          scopedSlots: { customRender: 'operate' }
        }
      ],
      pageData: {},
      loading: false,
      pageFilter: {
        pageNo: 1,
        pageSize: 10
      },
      table: {},
      interface:undefined,
      informationInterface:undefined
    }
  },
  mounted() {
    this.refreshTable(this.pageFilter)
  },
  methods: {
    handleOk2() {
      this.$refs.transfer.planning()
    },
    listLoook(val) {
      console.log(val)
      if(val.processType === "案(事)件" || val.processType === "启动预案"){
        let Strarr = []
        Strarr = val.taskId.split('_')
        if(val.processType === "启动预案"){
          val.taskId = val.taskId.split('_')[1]
          // alert(val.taskId.split('_')[2])
          console.log(Strarr)
        }
        let item = ''
        if (val.taskType === '命案防控' || Strarr[2] === '命案防控' ) item = 'Mi'
        if (val.taskType === '矛盾纠纷' || Strarr[2] === '矛盾纠纷' ) item = 'Ma'
        if (val.taskType === '线路案事件' || Strarr[2] === '线路案事件' ) item = 'Xi'
        if (val.taskType === '师生安全案事件' || Strarr[2] === '师生安全案事件' ) item = 'Te'
        if (val.taskType === '重特大案事件' || Strarr[2] === '重特大案事件' ) item = 'Zh'
        if (item !== '') {
          this.$router.push({
            path: '/LifeCase/detailed',
            query: {
              processStatus: val.processStatus,
              situation: 'ME',
              item: item,
              id: val.taskId,
              caozuo: val
            }
          })
        } else {
          this.$message.error('出现了意料之外的错误')
        }
      }

      if(val.processType === "物资调用"){
        // alert(6)
        this.$router.push({
          path:'/AgencyDetails',
          query:{
            // name:record.storageLocation,
            taskId:val.taskId
          }
        })
      }

    },
    chexiao(val) {
      console.log(val)
      if (val.processType == '案(事)件') {
        val.processType = 1
      }
      if (val.processType == '启动预案') {
        val.processType = 3
      }
      if (val.processType == '物资调用') {
        val.processType = 2
      }
      let data = {
        incidentName: val.title,
        taskId: val.taskId,
        type: val.processType,
        taskType:val.taskType
      }
      console.log(data)
      putCancel(data)
        .then(res => {
          console.log(res)
          if (res.success) {
            this.$message.success('撤销成功')
            this.table = res.result.records
            this.loading = false
            this.refreshTable(this.pageFilter)
          } else {
            this.$message.warn('请求失败！')
            this.loading = false
          }
        })
        .catch(e => {
          this.loading = false
          this.$message.warn('请求失败！')
        })
    },
    chongxin(val) {
      console.log(val)
      if (val.processType === '案(事)件') {

        if(val.taskType === '命案防控'){
          this.$router.push({
            path:'/LifeCase/upload',
            query:{
              type:val.taskType,
              processType:val.processType,
              id:val.taskId
            }
          })
        }
        if(val.taskType === '矛盾纠纷'){
          this.$router.push({
            path:'/contradiction/upload',
            query:{
              type:val.taskType,
              processType:val.processType,
              id:val.taskId
            }
          })
        }
        if(val.taskType === '线路案事件'){
          this.$router.push({
            path:'/route/upload',
            query:{
              type:val.taskType,
              processType:val.processType,
              id:val.taskId
            }
          })
        }
        if(val.taskType === '师生安全案事件'){
          this.$router.push({
            path:'/teachSchool/upload',
            query:{
              type:val.taskType,
              processType:val.processType,
              id:val.taskId
            }
          })
        }
        if(val.taskType === '重特大案事件'){
          this.$router.push({
            path:'/great/upload',
            query:{
              type:val.taskType,
              processType:val.processType,
              id:val.taskId
            }
          })
        }
      }
      if (val.processType === '启动预案') {
        let Strarr = []
        Strarr = val.taskId.split('_')
        if(Strarr[2] === '命案防控'){
          this.$router.push({
            path:'/LifeCase/detailed',
            query:{
              item:'Mi',
              type:val.taskType,
              processType:val.processType,
              id:val.taskId,
              title:Strarr[1]
            }
          })
        }
        if(Strarr[2] === '矛盾纠纷'){
          this.$router.push({
            path:'/LifeCase/detailed',
            query:{
              item:'Ma',
              type:val.taskType,
              processType:val.processType,
              id:Strarr[0],
              title:Strarr[1]
            }
          })
        }
        if(Strarr[2] === '线路案事件'){
          this.$router.push({
            path:'/LifeCase/detailed',
            query:{
              item:'Xi',
              type:val.taskType,
              processType:val.processType,
              id:Strarr[0],
              title:Strarr[1]
            }
          })
        }

        if(Strarr[2] === '师生安全案事件'){
          this.$router.push({
            path:'/LifeCase/detailed',
            query:{
              item:'Te',
              type:val.taskType,
              processType:val.processType,
              id:Strarr[0],
              title:Strarr[1]
            }
          })
        }
        if(Strarr[2] === '重特大案事件'){
          this.$router.push({
            path:'/LifeCase/detailed',
            query:{
              item:'Zh',
              type:val.taskType,
              processType:val.processType,
              id:Strarr[0],
              title:Strarr[1]
            }
          })
        }
      }
      if (val.processType === '物资调用') {
        reissueDetails(val.taskId).then(ser => {
          console.log(ser)
          if(ser.success){
            this.id = ser.result.information.materialId
            this.storageLocation = ser.result.information.storageLocation
            this.interface = ser.result.transfers
            this.informationInterface = ser.result.information
          }
        }).catch( err => {
          console.log(err)
        })
        this.visible2 = true
      }

    },
    objData(val) {
      console.log(val)
      if(val){
        this.visible2 = false
        this.refreshTable(this.pageFilter)
      }
    },
    onTableChange(pagination, pageFilter, sorter) {
      let keyMap = { currentPage: 'pageNo' }
      let paginations = Object.keys(pagination).reduce((newData, key) => {
        let newKey = keyMap[key] || key
        newData[newKey] = pagination[key]
        return newData
      }, {})
      this.pageFilter = Object.assign({}, this.pageFilter, paginations)
      this.refreshTable(Object.assign({}, this.pageFilter, this.data))
    },
    clickCustomRow(record, index) {
      var _this = this
      return {
        on: {
          // 事件
          click: () => {
            console.log(record)
            this.$emit('clickRow', record)
          }
        }
      }
    },
    query() {
      this.refreshTable(Object.assign({}, this.pageFilter, this.data))
    },

    refreshTable(val) {
      this.loading = true
      getMineList(val)
        .then(res => {
          console.log(res)
          if (res.success) {
            this.table = res.result
            this.loading = false
          } else {
            this.loading = false
          }
        })
        .catch(e => {
          this.loading = false
          this.$message.warn('请求失败！')
        })
    },

    onChange1(e) {
      console.log(e.target.value)
      if (e.target.value == '1') {
        this.data.state = ''
      } if (e.target.value == '2') {
        this.data.state = '进行中'
      } if (e.target.value == '3') {
        this.data.state = '已完结'
      }
    },
    onChange(date, dateString) {
      this.data.date = dateString
    },
    handleChange1(val) {
      this.data.type = val
      return val
    },
    handleChange2(val) {
      this.data.processStatus = val
      return val
    },
    filterMultiDictText(dictCode, text) {
      console.log(dictCode, text)
      if (!text && text !== 0) return
      let datas = getDictItemsFromCache(dictCode)
      // return this.$filterMultiDictText(datas, text)
      return this.$filterDictText(datas, text)
    }
  }
}
</script>

<style scoped>
.gutter-row{
  display: flex;
  align-items: center;
}
</style>
